<template>
  <div class="login-container">
    <div class="leftTitle">
      <div>评估辅助系统</div>
      <div style="font-size: 20px; margin-top: 10px">
        <!-- Common front-end framework -->
      </div>
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">账号登录</h3>
      </div>

      <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="userName"
          v-model="loginForm.userName"
          placeholder="用户名"
          name="userName"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-form-item prop="code" class="code" v-if="url">
        <div class="left">
          <span class="svg-container">
            <i class="el-icon-key"></i>
          </span>
          <el-input
            ref="code"
            v-model="loginForm.checkCode"
            placeholder="验证码"
            name="checkCode"
            type="text"
            tabindex="3"
            auto-complete="on"
          />
        </div>
        <el-image @click="getData" class="img" :src="url" fit="fill">
        </el-image>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script>
import {
  VerificationCode,
  VerificationCodeImg,
  getNlQueryFsUrl,
} from "@/api/management/user";
import { systemCode } from "@/utils";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        userName: "",
        password: "",
        checkCode: "",
      },
      loginRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 32, message: "请输入6-32位", trigger: "blur" },
        ],
        checkCode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      url: "",
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      // 获取验证码 bcode=0表示数据成功，bdata=1开启验证码
      let res1 = await VerificationCode();
      if (res1.data.bcode == 0 && res1.data.bdata == 1) {
        let res2 = await VerificationCodeImg();
        // 返回的是blob对象，转成图片地址
        let img = URL.createObjectURL(res2);
        this.url = img;
        this.loginForm.checkCode = "";
      }
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.loginForm.systemCode = systemCode;
          this.loginForm.checkCodeId = this.$store.state.user.checkcodeid;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.loginForm = {
                userName: "",
                password: "",
                code: "",
              };
              this.loading = false;
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch((response) => {
              this.loading = false;
              if (response.data && response.data.bmsg == "验证码不正确") {
                // 如果要刷新验证码在这里处理逻辑
                console.log("验证码不正确");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-image {
    cursor: pointer;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      // caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #e5e5e5 inset !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url("../../assets/login/login.png") no-repeat left top;
  background-size: 108%;
  overflow: hidden;

  .leftTitle {
    color: #fff;
    font-size: 40px;
    position: absolute;
    top: 50px;
    left: 100px;
  }

  .login-form {
    position: absolute;
    width: 424px;
    max-width: 100%;
    padding: 17px 31px 21px;
    margin: 0 auto;
    overflow: hidden;
    background-color: #fff;
    right: 359px;
    top: 50%;
    transform: translateY(-50%);
    border-radius: 10px;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: black;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .code {
    ::v-deep .el-form-item__content {
      display: flex !important;
      .left {
        flex: 1;
      }
      .img {
        width: 80px;
        height: 52px;
      }
    }
  }
}
</style>
